package com.csit.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author longweier
 * @Description: 日期工具类
 * @Copyright: 福州骏华科技信息有限公司 (c)2012
 * @Created Date : 2012-3-27
 * @vesion 1.0
 */
public class DateUtils {
	private static Map<String, SimpleDateFormat> formats;
	private static final String DATE_FORMATE_STRING_DEFAULT = "yyyyMMddHHmmss";
	public static final String DATE_FORMATE_STRING_A = "yyyy-MM-dd HH:mm:ss";
	private static final String DATE_FORMATE_STRING_B = "yyyy-MM-dd";
	private static final String DATE_FORMATE_STRING_C = "MM/dd/yyyy HH:mm:ss a";
	private static final String DATE_FORMATE_STRING_D = "yyyy-MM-dd HH:mm:ss a";
	private static final String DATE_FORMATE_STRING_E = "yyyy-MM-dd'T'HH:mm:ss'Z'";
	private static final String DATE_FORMATE_STRING_F = "yyyy-MM-dd'T'HH:mm:ssZ";
	private static final String DATE_FORMATE_STRING_G = "yyyy-MM-dd'T'HH:mm:ssz";
	private static final String DATE_FORMATE_STRING_H = "yyyyMMdd";
	private static final String DATE_FORMATE_STRING_I = "yyyy-MM-dd HH:mm:ss.SSS";
	private static final String DATE_FORMATE_STRING_J = "yyyyMMddHHmmss.SSS";

	private static final String SEPARATOR = "/"; //根据需求可改成-

	static {

		formats = new HashMap<String, SimpleDateFormat>();

		formats.put(DATE_FORMATE_STRING_DEFAULT, new SimpleDateFormat(DATE_FORMATE_STRING_DEFAULT));
		formats.put(DATE_FORMATE_STRING_A, new SimpleDateFormat(DATE_FORMATE_STRING_A));
		formats.put(DATE_FORMATE_STRING_B, new SimpleDateFormat(DATE_FORMATE_STRING_B));
		formats.put(DATE_FORMATE_STRING_C, new SimpleDateFormat(DATE_FORMATE_STRING_C));
		formats.put(DATE_FORMATE_STRING_D, new SimpleDateFormat(DATE_FORMATE_STRING_D));
		formats.put(DATE_FORMATE_STRING_E, new SimpleDateFormat(DATE_FORMATE_STRING_E));
		formats.put(DATE_FORMATE_STRING_F, new SimpleDateFormat(DATE_FORMATE_STRING_F));
		formats.put(DATE_FORMATE_STRING_G, new SimpleDateFormat(DATE_FORMATE_STRING_G));
		formats.put(DATE_FORMATE_STRING_H, new SimpleDateFormat(DATE_FORMATE_STRING_H));
		formats.put(DATE_FORMATE_STRING_I, new SimpleDateFormat(DATE_FORMATE_STRING_I));
		formats.put(DATE_FORMATE_STRING_J, new SimpleDateFormat(DATE_FORMATE_STRING_J));
	}

	/**
	 * @param
	 * @param date
	 * @return
	 * @throws Exception
	 * @Description: 将日期格式话为yyyy-MM-dd格式的字符串
	 * @Create: 2012-3-27 下午12:42:19
	 * @author longweier
	 * @update logs
	 */
	public static final String getDateString(Date date) {
		if(date==null){
			return "";
		}
		SimpleDateFormat sDateFormat = getDateFormat(DATE_FORMATE_STRING_B);
		return sDateFormat.format(date);
	}

	/**
	 * @param
	 * @param date
	 * @return
	 * @throws Exception
	 * @Description: 将日期格式话为yyyy-MM-dd HH:mm:ss格式的字符串
	 * @Create: 2012-3-27 下午12:42:51
	 * @author longweier
	 * @update logs
	 */
	public static final String getDateTimeString(Date date) {
		if(date==null){
			return "";
		}
		SimpleDateFormat sDateFormat = getDateFormat(DATE_FORMATE_STRING_A);
		return sDateFormat.format(date);

	}

	/**
	 * @param
	 * @param dateStr
	 * @return
	 * @throws Exception
	 * @Description: 将字符串转换为yyyy-MM-dd格式的日期
	 * @Create: 2012-3-27 下午12:43:21
	 * @author longweier
	 * @update logs
	 */
	public static final Date getDate(String dateStr) {
		Date date = null;
		try {
			return getDateFromString(dateStr, DATE_FORMATE_STRING_B);
		} catch (ParseException e) {
			e.printStackTrace();
			date = new Date();
		}
		return date;

	}

	/**
	 * @param
	 * @param date
	 * @param pattern
	 * @return
	 * @throws Exception
	 * @Description: 将日期转换为自定义的格式
	 * @Create: 2012-3-27 下午12:44:07
	 * @author longweier
	 * @update logs
	 */
	public static String getFormatTimeString(Date date, String pattern) {
		if(date==null){
			return "";
		}
		SimpleDateFormat sDateFormat = getDateFormat(pattern);
		return sDateFormat.format(date);
	}

	/**
	 * @param
	 * @param date
	 * @return
	 * @throws Exception
	 * @Description: 将日期转换为YYYYMMDDHHMMSS的格式
	 * @Create: 2012-3-27 下午12:44:47
	 * @author longweier
	 * @update logs
	 */
	public static String getDefaultFormateTimeString(Date date) {
		return getFormatTimeString(date, DATE_FORMATE_STRING_DEFAULT);
	}

	/**
	 * @param
	 * @param pattern
	 * @return
	 * @throws Exception
	 * @Description: 获取一个格式转换器
	 * @Create: 2012-3-27 下午12:45:05
	 * @author longweier
	 * @update logs
	 */
	public static SimpleDateFormat getDateFormat(String pattern) {
		SimpleDateFormat sDateFormat = formats.get(pattern);
		if (sDateFormat == null) {
			sDateFormat = new SimpleDateFormat(pattern);
			formats.put(pattern, sDateFormat);
		}
		return sDateFormat;
	}

	/**
	 * @param
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 * @throws Exception
	 * @Description: 将格式将日期字符串转换为Date对象
	 * @Create: 2012-3-27 下午12:45:39
	 * @author longweier
	 * @update logs
	 */
	public static Date getDateFromString(String date, String pattern) throws ParseException {
		if(date==null){
			return new Date();
		}
		SimpleDateFormat sDateFormat = getDateFormat(pattern);
		return sDateFormat.parse(date);
	}

	/**
	 * @param
	 * @param date
	 * @return
	 * @throws ParseException
	 * @throws Exception
	 * @Description: 将日期字符串转化成默认格式YYYYMMDDHHMMSS的Date对象
	 * @Create: 2012-3-27 下午12:45:50
	 * @author longweier
	 * @update logs
	 */
	public static Date getDefaultDateFromString(String date) throws ParseException {
		return getDateFromString(date, DATE_FORMATE_STRING_DEFAULT);
	}

	/**
	 * @param
	 * @return
	 * @throws Exception
	 * @Description: 取当前时间, 格式为YYYYMMDDHHMMSS的日期字符串
	 * @Create: 2012-3-27 下午12:46:03
	 * @author longweier
	 * @update logs
	 */
	public static String getNowDefault() {
		return getNow(DATE_FORMATE_STRING_DEFAULT);
	}

	/**
	 * @param
	 * @param pattern
	 * @return
	 * @throws Exception
	 * @Description: 按照pattern格式取当前日期字符串
	 * @Create: 2012-3-27 下午12:46:16
	 * @author longweier
	 * @update logs
	 */
	public static String getNow(String pattern) {
		return getFormatTimeString(new Date(), pattern);
	}

	/**
	 * @param
	 * @return
	 * @throws Exception
	 * @Description: 取当前时间, 格式为YYYYMMDD
	 * @Create: 2012-3-27 下午12:46:35
	 * @author longweier
	 * @update logs
	 */
	public static String getNowII() {
		return getFormatTimeString(new Date(), DATE_FORMATE_STRING_H);
	}

	/**
	 * @param
	 * @param str
	 * @param pattern
	 * @return
	 * @throws ParseException
	 * @throws Exception
	 * @Description: 将输入pattern格式的日期字符串转换为取时间的毫秒数 since 1976
	 * @Create: 2012-3-27 下午12:46:48
	 * @author longweier
	 * @update logs
	 */
	public static long dateString2Long(String str, String pattern) throws ParseException {
		return getDateFromString(str, pattern).getTime();
	}

	/**
	 * @param
	 * @param time
	 * @return
	 * @throws Exception
	 * @Description: 把since1976的毫秒数转成默认格式yyyyMMddHHmmss的String日期字符串
	 * @Create: 2012-3-27 下午12:46:57
	 * @author longweier
	 * @update logs
	 */
	public static String longToDateStringDefault(long time) {
		return getFormatTimeString(new Date(time), DATE_FORMATE_STRING_DEFAULT);
	}

	/**
	 * @param
	 * @param time
	 * @param pattern
	 * @return
	 * @throws Exception
	 * @Description: 将时间的毫秒数 since 1976转换为按照pattern格式的日期字符串
	 * @Create: 2012-3-27 下午12:47:15
	 * @author longweier
	 * @update logs
	 */
	public static String longToDateString(long time, String pattern) {
		return getFormatTimeString(new Date(time), pattern);
	}

	/**
	 * @param
	 * @param date
	 * @return
	 * @throws Exception
	 * @Description: 将Date对象转成since 1976的毫秒数
	 * @Create: 2012-3-27 下午12:47:30
	 * @author longweier
	 * @update logs
	 */
	public static long date2Long(Date date) {
		if(date==null){
			return 0L;
		}
		return date.getTime();
	}

	/**
	 * @param
	 * @param time
	 * @return
	 * @throws Exception
	 * @Description: 将since1976毫秒数转成Date对象
	 * @Create: 2012-3-27 下午12:47:38
	 * @author longweier
	 * @update logs
	 */
	public static Date longToDate(long time) {
		return new Date(time);
	}

	/**
	 * @param
	 * @param date
	 * @return
	 * @throws ParseException
	 * @throws Exception
	 * @Description: 自动适配两种格式的日期字符串转换为date对象
	 * @Create: 2012-3-27 下午12:47:49
	 * @author longweier
	 * @update logs
	 */
	public static Date getDateFromStringAdaptTwoPattern(String date) throws ParseException {
		try {
			return getDateFromString(date, DATE_FORMATE_STRING_A);
		} catch (ParseException e) {
			return getDateFromString(date, DATE_FORMATE_STRING_B);
		}
	}

	/**
	 * @param
	 * @param dateStr
	 * @return
	 * @throws Exception
	 * @Description: 判断当前时间是否小于dateStr时间
	 * @Create: 2012-3-27 下午12:48:18
	 * @author longweier
	 * @update logs
	 */
	public static boolean isDateBefore(String dateStr) {
		Date date = new Date();
		try {
			DateFormat df = DateFormat.getDateInstance();
			return df.parse(dateStr).before(date);
		} catch (ParseException e) {
			return false;
		}
	}

	/**
	 * @param
	 * @param params
	 * @return
	 * @throws Exception
	 * @Description: 根据Map里面的year、month、day拼凑成当前日期 yyyy/MM/dd
	 * @Create: 2012-3-27 下午12:48:35
	 * @author longweier
	 * @update logs
	 */
	public static String getDateByYearMonthDay(Map<String, Object> params) {

		return params.get("year") + SEPARATOR + params.get("month") + SEPARATOR + params.get("day");
	}


	/**
	 * 日期串转为日期对象
	 *
	 * @param dateString 日期串
	 * @return Date对象
	 */
	public static Date getDateFromString(String dateString) {
		Set<String> set = formats.keySet();
		for (String format : set) {
			try {
				return getDateFromString(dateString, format);
			} catch (ParseException e) {
				//nothing .ignore
			}
		}
		return null;
	}
}
